home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / agg / Set.c < prev   
C/C++ Source or Header  |  1992-01-23  |  13KB  |  376 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. // **************************************************************************
  12. // Module Set                       03/07/89           Bernhard Schiefer (bs)
  13. //                                                     modified: 23/7/90 (ja)
  14. //                                                              24/10/91 (bs)
  15. // **************************************************************************
  16. // implements methods of classes: Set
  17. // **************************************************************************
  18.  
  19. #include "agg_err.h"
  20. #include "trc_agg.h"
  21. #include "sys.h"
  22.  
  23. #include "agg_sos.h"
  24.  
  25. // *** Sets are based on Object_sos_Bool_Mappings ***
  26.  
  27. // **************************************************************************
  28. void sos_Object_Set::local_initialize (sos_Object_Set set)
  29. // **************************************************************************
  30. {  T_PROC ("sos_Object_Set::local_initialize");
  31.    TT (agg_H, T_ENTER);
  32.  
  33.    set.set_m (sos_Object_sos_Bool_Mapping::create
  34.          (set.container(),
  35.           set.get_list_cursor(),
  36.           set.get_based_on_equal()));
  37.  
  38.    TT (agg_H, T_LEAVE);
  39. } // ** local_initialize **
  40.  
  41. // **************************************************************************
  42. void sos_Object_Set::local_finalize (sos_Object_Set set)
  43. // **************************************************************************
  44. {  T_PROC ("sos_Object_Set::local_finalize");
  45.    TT (agg_H, T_ENTER);
  46.  
  47.    set.get_m().destroy();
  48.  
  49.    TT (agg_H, T_LEAVE);
  50. } // ** local_finalize **
  51.  
  52. // **************************************************************************
  53. void sos_Object_Set::insert (sos_Object o)
  54. // **************************************************************************
  55. // fuege das Objekt o in das Set ein, es wird die Anzahl der danach im
  56. // Set vorhandenen Exemplare geliefert.
  57. {
  58.    T_PROC ("sos_Object_Set::insert");
  59.    TT (agg_H, T_ENTER);
  60.  
  61.    sos_Object_sos_Bool_Mapping m = self.get_m();
  62.    m.insert (o, TRUE);
  63.  
  64.    TT (agg_H, T_LEAVE);
  65. } // ** insert **
  66.  
  67. // **************************************************************************
  68. void sos_Object_Set::remove (sos_Object o)
  69. // **************************************************************************
  70. // loesche ein Objekt o, es wird TRUE,geliefert, falls es drin war
  71. {  
  72.    T_PROC ("sos_Object_Set::remove");
  73.    TT (agg_H, T_ENTER);
  74.  
  75.    self.get_m().remove (o);
  76.  
  77.    TT (agg_H, T_LEAVE);
  78. } // ** remove **
  79.  
  80. // **************************************************************************
  81. void sos_Object_Set::operator+= (sos_Object_Set aset)
  82. // **************************************************************************
  83. // Nach A += B wurden alle Elemente in B zu A aufaddiert
  84. {
  85.    T_PROC ("sos_Object_Set::operator+=");
  86.    TT (agg_H, T_ENTER);
  87.  
  88.    sos_Object_sos_Bool_Mapping this_m = self.get_m();
  89.    sos_Object_sos_Bool_Mapping aset_m = aset.get_m();
  90.  
  91.    sos_Cursor cur = aset_m.open_cursor ();
  92.    for (;aset_m.is_valid (cur); aset_m.to_succ (cur))
  93.    {  sos_Object o = aset_m.get_key (cur);
  94.       this_m.insert (o, TRUE);
  95.    } // for
  96.    aset_m.close_cursor (cur);
  97.  
  98.    TT (agg_H, T_LEAVE);
  99. } // ** operator+= **
  100.  
  101. // **************************************************************************
  102. void sos_Object_Set::operator-= (sos_Object_Set aset)
  103. // **************************************************************************
  104. // Nach A -= B wurden aus A alle Elemente, die in B sind, entfernt
  105. {  
  106.    T_PROC ("sos_Object_Set::operator-=");
  107.    TT (agg_H, T_ENTER);
  108.  
  109.    sos_Object_sos_Bool_Mapping this_m = self.get_m();
  110.    sos_Object_sos_Bool_Mapping aset_m = aset.get_m();
  111.  
  112.    sos_Cursor cur = aset_m.open_cursor();
  113.    for (;aset_m.is_valid (cur);aset_m.to_succ (cur))
  114.    {  sos_Object o = aset_m.get_key (cur);
  115.       this_m.remove (o);
  116.    } // for
  117.    aset_m.close_cursor (cur);
  118.  
  119.    TT (agg_H, T_LEAVE);
  120. } // ** operator-= **
  121.  
  122. // **************************************************************************
  123. void sos_Object_Set::operator*= (sos_Object_Set aset)
  124. // **************************************************************************
  125. // Liefert die Schnittmenge von A und B in A,
  126. // Also entferne aus self alle Elemente, die nicht in aset sind
  127. {
  128.    T_PROC ("sos_Object_Set::operator*=");
  129.    TT (agg_H, T_ENTER);
  130.  
  131.    sos_Object_sos_Bool_Mapping aset_m = aset.get_m();
  132.    sos_Object_sos_Bool_Mapping this_m = self.get_m();
  133.  
  134.    sos_Cursor cur = this_m.open_cursor();
  135.    for (;this_m.is_valid (cur);)
  136.    {  sos_Object o = this_m.get_key (cur);
  137.       if (NOT aset_m.is_key (o))
  138.          this_m.remove_at (cur);
  139.       else
  140.          this_m.to_succ (cur);
  141.    } // for 
  142.    this_m.close_cursor (cur);
  143.  
  144.    TT (agg_H, T_LEAVE);
  145. } // ** operator*= **
  146.  
  147. // **************************************************************************
  148. sos_Bool sos_Object_Set::operator< (sos_Object_Set aset)
  149. // **************************************************************************
  150. // Liefert TRUE zurueck, wenn jedes Element aus this auch in aset ist
  151. // und aset mindestens ein Element mehr enthaelt
  152.    T_PROC ("sos_Object_Set::operator<");
  153.    TT (agg_H, T_ENTER);
  154.  
  155.    sos_Object_sos_Bool_Mapping aset_m = aset.get_m();
  156.    sos_Object_sos_Bool_Mapping this_m = self.get_m();
  157.    sos_Bool result = TRUE;
  158.  
  159.    if (this_m.card() >= aset_m.card())
  160.       result = FALSE;
  161.    else
  162.    {  sos_Cursor cur = this_m.open_cursor();
  163.       for (;this_m.is_valid (cur);this_m.to_succ (cur))
  164.       {  sos_Object o = this_m.get_key (cur);
  165.          if (NOT aset_m.is_key (o))
  166.          {  result = FALSE;
  167.             break;
  168.          }
  169.       } // for
  170.       this_m.close_cursor (cur);
  171.    }
  172.  
  173.    TT (agg_H, T_LEAVE);
  174.    return result;
  175. } // ** operator< **
  176.  
  177. // **************************************************************************
  178. sos_Bool sos_Object_Set::operator<= (sos_Object_Set aset)
  179. // **************************************************************************
  180. // Liefert TRUE zurueck, wenn jedes Element aus this auch in aset ist
  181.    T_PROC ("sos_Object_Set::operator<=");
  182.    TT (agg_H, T_ENTER);
  183.  
  184.    sos_Object_sos_Bool_Mapping aset_m = aset.get_m();
  185.    sos_Object_sos_Bool_Mapping this_m = self.get_m();
  186.    sos_Bool result = TRUE;
  187.  
  188.    // pruefe zuerst, ob die Anzahl der Elemente schon ein Ergebnis liefert
  189.    if (this_m.card() > aset_m.card())
  190.       result = FALSE;
  191.    else
  192.    {  sos_Cursor cur = this_m.open_cursor();
  193.       for (;this_m.is_valid (cur); this_m.to_succ (cur))
  194.       {  sos_Object o = this_m.get_key (cur);
  195.          if (NOT aset_m.is_key (o))
  196.          {  result = FALSE;
  197.             break;
  198.          }
  199.       } // for
  200.       this_m.close_cursor (cur);
  201.    }
  202.    TT (agg_H, T_LEAVE);
  203.    return result;
  204. } // ** operator<= **
  205.  
  206. // **************************************************************************
  207. sos_Bool sos_Object_Set::operator> (sos_Object_Set aset)
  208. { return sos_Bool (aset < self); }
  209. // **************************************************************************
  210.  
  211. // **************************************************************************
  212. sos_Bool sos_Object_Set::operator>= (sos_Object_Set aset)
  213. { return sos_Bool (aset <= self); }
  214. // **************************************************************************
  215.  
  216. // **************************************************************************
  217. void sos_Object_Set::local_assign (sos_Object_Set x, sos_Object o)
  218. // **************************************************************************
  219. {  T_PROC ("sos_Object_Set::local_assign");
  220.    TT(agg_H, T_ENTER);
  221.  
  222.    sos_Object_Set y = sos_Object_Set::make (o);
  223.    x.get_m().assign (y.get_m());
  224.  
  225.    TT(agg_H, T_LEAVE);
  226. } // local_assign
  227.  
  228. // **************************************************************************
  229. sos_Bool sos_Object_Set::local_equal (sos_Object_Set x,
  230.                       sos_Object     o,
  231.                        sos_Eq_kind    eq_kind)
  232. // **************************************************************************
  233. {  T_PROC ("sos_Object_Set::local_equal");
  234.    TT(agg_H, T_ENTER);
  235.    
  236.    sos_Bool result;
  237.  
  238.    if ((eq_kind EQ EQ_STRONG AND NOT o.has_type (x.type())) OR
  239.        (eq_kind EQ EQ_WEAK   AND NOT o.isa      (x.type())))
  240.       result = FALSE;
  241.    else
  242.    {  sos_Object_Set y = sos_Object_Set::make (o);
  243.       result = x.get_m().equal (y.get_m(), eq_kind);
  244.    }
  245.  
  246.    TT(agg_H, T_LEAVE);
  247.    return result;
  248. } // local_equal
  249.  
  250. // **************************************************************************
  251. sos_Int sos_Object_Set::local_hash_value (sos_Object_Set x)
  252. // **************************************************************************
  253. {  T_PROC ("sos_Object_Set::local_hash_value");
  254.    TT(agg_H, T_ENTER);
  255.    
  256.    sos_Int result = x.get_m().hash_value ();
  257.  
  258.    TT(agg_H, T_LEAVE);
  259.  
  260.    return result;
  261. } // local_hash_value
  262.  
  263. // **************************************************************************
  264. sos_Bool sos_Object_Set::is_element (sos_Object o)
  265. // **************************************************************************
  266. {  T_PROC ("sos_Object_Set::is_element");
  267.    TT (agg_H, T_ENTER);
  268.  
  269.    sos_Bool result = self.get_m().is_key (o);
  270.  
  271.    TT (agg_H, T_LEAVE);
  272.    return result;
  273. } // ** is_element(sos_Object)
  274.  
  275. // **************************************************************************
  276. sos_Object sos_Object_Set::get (sos_Cursor c)
  277. // **************************************************************************
  278. {  T_PROC ("sos_Object_Set::get");
  279.    TT (agg_H, T_ENTER);
  280.  
  281.    sos_Object o = self.get_m().get_key (c);
  282.  
  283.    TT (agg_H, T_LEAVE);
  284.    return o;
  285. } // ** get **
  286.  
  287. // **************************************************************************
  288. void sos_Object_Set::remove_at (sos_Cursor c)
  289. // **************************************************************************
  290. {  T_PROC ("sos_Object_Set::remove_at");
  291.    TT (agg_H, T_ENTER);
  292.  
  293.    self.get_m().remove_at (c);
  294.  
  295.    TT (agg_H, T_LEAVE);
  296. } // ** remove_at **
  297.  
  298. // **************************************************************************
  299. void sos_Object_Set::clear()
  300. // **************************************************************************
  301. {  T_PROC ("sos_Object_Set::clear");
  302.    TT (agg_H, T_ENTER);
  303.    
  304.    self.get_m().clear();
  305.  
  306.    TT (agg_H, T_LEAVE);
  307. } // ** clear
  308.  
  309. // **************************************************************************
  310. sos_Int sos_Object_Set::card ()
  311. // **************************************************************************
  312. {  return self.get_m().card ();
  313. } // card
  314.  
  315. // **************************************************************************
  316. sos_Cursor sos_Object_Set::open_cursor (sos_Container Cursor_ct)
  317. // **************************************************************************
  318. {  return self.get_m().open_cursor (Cursor_ct);
  319. } // open_cursor
  320.  
  321. // **************************************************************************
  322. void sos_Object_Set::close_cursor (sos_Cursor c)
  323. // **************************************************************************
  324. {  self.get_m().close_cursor (c);
  325. } // close_cursor
  326.  
  327. // **************************************************************************
  328. sos_Cursor sos_Object_Set::duplicate (sos_Cursor c)
  329. // **************************************************************************
  330. {  return self.get_m().duplicate (c);
  331. } // duplicate
  332.  
  333. // **************************************************************************
  334. sos_Bool sos_Object_Set::is_valid (sos_Cursor c)
  335. // **************************************************************************
  336. {  return self.get_m().is_valid (c);
  337. } // ** is_valid **
  338.  
  339. // **************************************************************************
  340. sos_Bool sos_Object_Set::to_first (sos_Cursor c)
  341. // **************************************************************************
  342. {  return self.get_m().to_first (c);
  343. } // to_first
  344.  
  345. // **************************************************************************
  346. sos_Bool sos_Object_Set::to_last (sos_Cursor c)
  347. // **************************************************************************
  348. {  return self.get_m().to_last (c);
  349. } // to_last
  350.  
  351. // **************************************************************************
  352. sos_Bool sos_Object_Set::to_succ (sos_Cursor c, sos_Int steps)
  353. // **************************************************************************
  354. {  T_PROC ("sos_Object_Set::to_succ");
  355.    TT (agg_H, T_ENTER);
  356.  
  357.    sos_Bool result = self.get_m().to_succ (c, steps);
  358.  
  359.    TT (agg_H, T_LEAVE);
  360.    return result;
  361. } // ** to_succ**
  362.  
  363. // **************************************************************************
  364. sos_Bool sos_Object_Set::to_pred (sos_Cursor c, sos_Int steps)
  365. // **************************************************************************
  366. {  T_PROC ("sos_Object_Set::to_pred");
  367.    TT (agg_H, T_ENTER);
  368.  
  369.    sos_Bool result = self.get_m().to_pred (c, steps);
  370.    
  371.    TT (agg_H, T_LEAVE);
  372.    return result;
  373. } // ** to_pred **
  374.